<?php

require_once 'luckyflagmodel.php';
/**
 * Description of TaskModel
 *
 * @author David Morris
 */
class TaskModel extends LuckyFlagModel
{
    public function __construct()
    {
        $this->table = 'tasks_table';
        parent::__construct();
    }
    
    /**
     * Get a list of user ids for users assigned to the task
     * 
     * @param type $taskId the task we want to find the users for
     * @return result 
     */
    public function returnUsers($taskId)
    {
        return $this->db->select('user_id')->from('tasks_to_users')->where('task_id = '.$taskId)->get()->result();
    }
    
    /**
     * Remove a user from a task, unassigning them.
     * 
     * @param int $userId the user id of the user we're detaching
     * @param int $taskId the id of the task to remove the user from
     * @return TaskModel 
     */
    public function detachUser($userId, $taskId)
    {
        if(!is_numeric($userId) || !is_numeric($taskId))
            throw new Exception ("No valid id provided for user or task");
        $this->db->delete('tasks_to_users', array('task_id' => $taskId, 'user_id' => $userId));
        return $this;
    }
    
    /**
     * Assigns users to a task. If no specific task id is provided it users the task id
     * of the last created task.
     * 
     * @param array $users array of user ids to assign
     * @param int $taskId id of the task to assign the users to
     * @return TaskModel 
     */
    public function assignUsers($users, $taskId = null)
    {
        // If it's a single user then put it an array, save extra code later
        if (!is_array($users))
            $users = array($users);
        // If it's null then use the last insertion ID
        if (is_null($taskId))
            $taskId = $this->insert_id();
        // If it's still null throw exception
        if (is_null($taskId))
            throw new Exception("No ID for task provided for users to be assigned to.");

        $insert = array();
        foreach ($users as $u)
            $insert[] = array('task_id' => $taskId, 'user_id' => $u);
        if (count($insert))
            $this->db->insert_batch('tasks_to_users', $insert);
        else
            throw new Exception("No users provided to be assigned to this task");
        return $this;
    }

    /**
     * Inserts a new task into the database. Optionally takes an array of user ids to assign
     * to the task as well.
     * 
     * @see LuckyFlagModel::insert()
     * @param array $users optional array of user ids to assign to the newly created task
     * @return TaskModel 
     */
    public function insert($users = null)
    {
        $this->columns['task_created'] = time();
        parent::insert();
        if (!is_null($users))
            $this->assignUsers($users, $this->insert_id());
        return $this;
    }

    /**
     * Deletes a note safely. Rather than just using the notes model to delete, this deletes
     * the link between the note and task.
     * 
     * @param type $taskId id of the task
     * @param type $noteId id of the note
     * @return TaskModel 
     */
    public function removeNote($taskId, $noteId)
    {
        $this->db->delete('tasks_notes', array('task_id' => $taskId, 'note_id' => $noteId));
        // TODO renable these once notes model created
        //$this->load->model('notesmodel','NotesModel');
        //$this->load->NotesModel->deleteNote($noteId);
        return $this;
    }

    /**
     * Assigns a note to a task. Joins tasks_table to notes_table
     * 
     * @param int $taskId id of the task
     * @param int $noteId id of the note
     * @return TaskModel 
     */
    public function assignNote($taskId, $noteId)
    {
        if (!is_numeric($taskId) || !is_numeric($noteId))
            throw new Exception("Not valid id's for task or note.");
        $this->db->insert('tasks_notes', array('note_id' => $noteId, 'task_id' => $taskId));
        return $this;
    }
    
    /**
     * Returns all tasks as an array, with the key being the id of the task and the value
     * the name of the task.
     * Optionally, you can pass in the id of a task to ignore
     * 
     * @param int $ignore
     * @return array 
     */
    public function getTasksAsArray($ignore = null)
    {
        $return = array();
        $extra = '';
        if(is_numeric($ignore))
            $extra = ' WHERE task_id != '.$ignore;
        $rows = $this->db->query('SELECT * FROM '.$this->table.$extra.' ORDER BY task_name');
        foreach($rows->result() as $row)
            $return[$row->task_id] = $row->task_name;
        return $return;
    }
    
    // TODO methods for attachments

}